<template>
    <div>
        <Card>
            <p slot="title" class="card-title">
                <Icon type="android-checkbox-outline"></Icon>
                组织机构权限
            </p>
            <Tree :data="buildOrganTree"></Tree>
        </Card>
    </div>
</template>
<script>
    import iviewCheck from '@/view/common/checkbox/Checkbox.vue'
    export default {
        components: {
            iviewCheck
        },
        data () {
            return {
                id: '',
                user: {},
                baseURL: '/user/organ',
                appendName: '无',
                organs: [],
                buttonProps: {
                    type: 'ghost',
                    size: 'small',
                },
                formItem: {
                    organCode: '',
                    organName: '',
                    parentId: '',
                    level: ''
                }
            }
        },
        computed: {
            buildOrganTree: function () {
                let organList = []
                this.organs.forEach(function (organ) {
                    let treeNode = {
                        id: organ.id,
                        title: organ.organName,
                        level: organ.level,
                        expand: organ.checked,
                        render: (h, { root, node, data }) => {
                            return h('span', {
                                style: {
                                    display: 'inline-block',
                                    fontWeight: 'bold',
                                    fontSize: '15px',
                                    color: '#2d8cf0',
                                    width: '100%'
                                }
                            }, [
                                h('span', [
                                    h(iviewCheck, {
                                        attrs: {
                                            // 定义静态属性
                                        },
                                        props: {
                                            state: organ.checked
                                        },
                                        style: {
                                            marginRight: '8px'
                                        },
                                        on: {
                                            'onChange': () => {
                                                this.onChange(organ)
                                            }
                                        }
                                    }),
                                    h('Icon', {
                                        props: {
                                            type: 'location'
                                        },
                                        style: {
                                            marginRight: '8px'
                                        }
                                    }),
                                    h('span', data.title)
                                ])
                            ]);
                        }
                    }
                    treeNode.children = []
                    organList.push(treeNode)
                    if (organ.children) {
                        this.getChildByOrgan(organ.children, treeNode.children)
                    }
                }, this);
                return organList
            }
        },
        methods: {
            getChildByOrgan (organs, children) {
                organs.forEach(function (organ) {
                    let colorer = '#19be6b'
                    // if (menu.type === 2) {
                    //     colorer = '#ff9900'
                    // }
                    let child = {
                        id: organ.id,
                        title: organ.organName,
                        level: organ.level,
                        expand: organ.checked,
                        render: (h, { root, node, data }) => {
                            return h('span', {
                                style: {
                                    display: 'inline-block',
                                    fontWeight: 'bold',
                                    fontSize: '15px',
                                    color: '#2d8cf0',
                                    width: '100%'
                                }
                            }, [
                                h('span', [
                                    h(iviewCheck, {
                                        attrs: {
                                            // 定义静态属性
                                        },
                                        props: {
                                            state: organ.checked
                                        },
                                        style: {
                                            marginRight: '8px'
                                        },
                                        on: {
                                            'onChange': () => {
                                                this.onChange(organ)
                                            }
                                        }
                                    }),
                                    h('Icon', {
                                        props: {
                                            type: 'location'
                                        },
                                        style: {
                                            marginRight: '8px'
                                        }
                                    }),
                                    h('span', data.title)
                                ])
                            ]);
                        }
                    }
                    child.children = []
                    children.push(child)
                    if (organ.children && organ.children.length > 0) {
                        this.getChildByOrgan(organ.children, child.children)
                    }
                }, this);
            },
            onChange (organ) {
                this.$bus.start()
                let url = this.baseURL + '/addOrgan/' + this.id
                this.$ajax.post(url, organ).then(function (response) {
                    let {code} = response.data
                    if (code === 0) {
                        this.init()
                        this.$bus.finish()
                    }
                }.bind(this)).then(function (error) {
                    if (error) {
                        this.$bus.error()
                    }
                }.bind(this))
            },
            init () {
                let url = this.baseURL + '/findByUser/' + this.id
                this.$ajax.get(url).then((response) => {
                    let {code, data} = response.data
                    if (code === 0) {
                        this.organs = data
                        this.buildOrganTree()
                    }
                }).then(function (error) {
                    if (error) {
                        this.$bus.error()
                    }
                }.bind(this))
            }
        },
        mounted () {
            this.init()
        },
        watch: {
            '$route' (to) {
                if (to.name === 'system_user_organ') {
                    this.init()
                }
            }
        }
    }
</script>